mariadb-openstack-pkg:
  pkg.installed:
    - names:
      - mariadb
      - mariadb-server
      - python2-PyMySQL

mariadb-openstack-conf:
  file.managed:
    - name: /etc/my.cnf.d/openstack.cnf
    - source: salt://openstack/queen/mariadb/files/openstack.cnf.template
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      IP_ADDR: {{ grains['fqdn_ip4'][0] }}
    - require:
      - pkg: mariadb-openstack-pkg

mariadb-service:
  service.running:
    - name: mariadb
    - enable: True
    - reload: True
    - require:
      - file: mariadb-openstack-conf
    - watch:
      - file: mariadb-openstack-conf

mariadb-secure-install:
  file.managed:
    - name: /etc/script/init-mysql.sh
    - source: salt://openstack/queen/mariadb/files/init-mysql.sh
    - user: root
    - group: root
    - mode: 744
    - template: jinja
    - defaults:
      ROOT_PASS: {{ pillar['mysql']['ROOT_PASS'] }}
  cmd.run:
    - name: bash /etc/script/init-mysql.sh && touch /etc/lock/mysql-init.lock
    - require:
      - file: mariadb-secure-install
    - unless: test -f /etc/lock/mysql-init.lock
